#include "mainloop.h"

MainLoop::MainLoop(QObject *parent) :
    QObject(parent)
{
    readConfig();
}

void MainLoop::readConfig()
{
    QSettings settings;

    login = settings.value("Acc/login").toString();
    password = settings.value("Acc/password").toString();
    server = settings.value("Acc/server").toString();
    port = settings.value("Acc/port").toString();
}

void MainLoop::createAccount()
{
    bool porterr = true;
    QSettings settings;
    QString line;
    QTextStream stream(stdin);
    QTextStream out(stdout);

    out << trUtf8("Введите логин:") << endl;
    settings.setValue("Acc/login", stream.readLine());
    out << trUtf8("Введите пароль:") << endl;
    settings.setValue("Acc/password", stream.readLine());
    out << trUtf8("Введите сервер:") << endl;
    settings.setValue("Acc/server", stream.readLine());
    do {
        out << trUtf8("Введите порт:") << endl;
        line = stream.readLine();
        QRegExp rx("^\\d+$");
        if (rx.indexIn(line) == 0) {
            porterr = false;
        } else {
            out << trUtf8("неверное значение ") << "\"" << line << "\"" << endl;
        }
    } while (porterr);
    settings.setValue("Acc/port", line);

    out.flush();
}

